<?php

/**
 * Class documents(签章文档管理)
 * Notes:
 * Author lv
 * Email 578530370@qq.com
 * Date 2020/8/4 9:01
 */
class documentsClassModel extends Model
{
    public function initModel()
    {
        $this->settable('zbcg_documents');
    }

    public function add($param)
    {
        $param['create_time'] = date('Y-m-d H:i:s');
        $param['update_time'] = date('Y-m-d H:i:s');
        return $this->insert($param);
    }

    public function download($path, $filename)
    {
        $path = trim($path, './');
        $path = $path.DIRECTORY_SEPARATOR.$filename;
        if (! file_exists ( $path )) {
            header('HTTP/1.1 404 NOT FOUND');
        } else {
            // http headers
            header('Content-Type: application-x/force-download');
            header("Content-Disposition: attachment; filename=" . $filename);
            header('Content-length: ' . filesize($path));
            // for IE6
            if (false === strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6')) {
                header('Cache-Control: no-cache, must-revalidate');
            }
            // 打开文件
            $fp = fopen($path, 'rb');
            // 设置指针位置
            fseek($fp, 0);
            // 开启缓冲区
            ob_start();
            // 分段读取文件
            while (!feof($fp)) {
                $chunk_size = 1024 * 1024 * 2; // 2MB
                echo fread($fp, $chunk_size);
                ob_flush(); // 刷新PHP缓冲区到Web服务器
                flush(); // 刷新Web服务器缓冲区到浏览器
                sleep(1); // 每1秒 下载 2 MB
            }
            // 关闭缓冲区
            ob_end_clean();
            fclose($fp);
        }
    }
}